// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'
import Client from '@api/contract/api/ProjectApi'

import ComClass from '@api/ComQuery'
import ListClass from '@api/contract/model/QueryProjectListReq'
import OneClass from '@api/contract/model/QueryProjectDetailReq'
import AddClass from '@api/contract/model/InsertProjectReq'
import EditClass from '@api/contract/model/UpdateProjectReq'
import DelClass from '@api/contract/model/DeleteProjectReq'
import BaseClass from '@api/contract/model/ProjectDto'
import FileClass from '@api/contract/model/ProjectFileDto'

const CLIENT = CORE.grpcCLIENT(Client)
const COM = [ComClass, 'comQuery']
const FILE = [FileClass, 'projectFileDto', 1]
const BASE = [BaseClass, 'projectDto', '', (dto, obj) => CORE.grpcDTO(dto, obj, FILE)]

// ============================== 导出远程接口 ============================== //

export default {
  // —————————————————————————————— 列表 —————————————————————————————— //

  async getList(param, TIP = '查询列表') {
    const DTO = CORE.grpcDTO(ListClass, param, COM, [BaseClass, 'projectDto'])
    const resp = await CORE.grpc(TIP, CLIENT, 'queryProjectList', DTO)
    return [resp.comQuery.totalCount, resp.projectDto || []]
  },

  // —————————————————————————————— 单项 —————————————————————————————— //

  async getOne({ id }, TIP = '查询详情') {
    const DTO = CORE.grpcDTO(OneClass, { id })
    const resp = await CORE.grpc(TIP, CLIENT, 'queryProjectDetail', DTO)
    return resp.projectDto || {}
  },

  // —————————————————————————————— 增 —————————————————————————————— //

  async add(one, TIP = '新增') {
    const DTO = CORE.grpcDTO(AddClass, one, BASE)
    await CORE.grpc(TIP, CLIENT, 'insertProject', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 改 —————————————————————————————— //

  async edit(one, TIP = '修改') {
    const DTO = CORE.grpcDTO(EditClass, one, BASE)
    await CORE.grpc(TIP, CLIENT, 'updateProject', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 删 —————————————————————————————— //

  async del({ id }, TIP = '删除') {
    const DTO = CORE.grpcDTO(DelClass, { ids: id })
    await CORE.grpc(TIP, CLIENT, 'deleteProject', DTO)
    return CORE.grpcOK(TIP)
  },
}
